class Solution {
public:
    vector<int> ret;
    int n;
    bool fun(int acc){
        if (acc>n) return false;
        ret.push_back(acc);
        for (int i = 0; i < 10; i++)
            if (!fun(acc*10+i)) 
                break;
        return true;
    }
    vector<int> lexicalOrder(int n) {
        ret.reserve(n);
        ret.clear();
        this->n = n;
        for (int i = 1; i < 10; i++) fun(i);
        return ret;
    }
};